package edu.whu.demo.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import edu.whu.demo.domain.Schedule;
import edu.whu.demo.domain.ScheduleView;
import edu.whu.demo.exception.ScheduleAdminException;
import edu.whu.demo.service.IScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2022-11-24
 */
@RestController
@RequestMapping("/schedule")
public class ScheduleController {

    @Autowired
    IScheduleService scheduleService;
    /*加入日程*/
    @PostMapping("/add")
    @PreAuthorize("hasRole('normal')")
    public ResponseEntity<Schedule> addSchedule(@RequestParam long uid,@RequestParam long activityId) throws ScheduleAdminException {
        return ResponseEntity.ok(scheduleService.addSchedule(uid,activityId));
    }
    /*新建日程*/
    @PostMapping("/create")
   @PreAuthorize("hasRole('normal')")

    public ResponseEntity<Schedule> createSchedule(@RequestBody Schedule schedule) throws ScheduleAdminException {
        return ResponseEntity.ok(scheduleService.createSchedule(schedule));
    }
    /*移除日程*/
    @DeleteMapping("/{id}")
   @PreAuthorize("hasRole('normal')")
    public void deleteSchedule(@PathVariable Long id){
        scheduleService.deleteSchedule(id);
    }

    /*修改日程*/
    @PutMapping("/update")
   @PreAuthorize("hasRole('normal')")
    public ResponseEntity<Schedule> updateScheduleUser(@RequestParam Long uid,@RequestParam Long id,@RequestBody Schedule schedule) throws ScheduleAdminException{
        return ResponseEntity.ok(scheduleService.updateScheduleUser(uid,id,schedule));
    }
    /*查看日程*/
    @GetMapping("")
    @PreAuthorize("hasRole('normal')")
    public IPage<ScheduleView> findSchedules(
            @RequestParam(defaultValue = "0") Long pageNum,
            @RequestParam(defaultValue = "10") Long pageSize,
            @RequestParam(required=false) Long uid,
            @RequestParam(required=false) String keyword,
            @RequestParam(required=false) String label,
            @RequestParam(required=false) String date,
            @RequestParam(required=false) Long activityId

    ) throws ScheduleAdminException {
           Map<String, Object> condition = new HashMap<>();
           if (keyword != null) {
               condition.put("keyword", keyword);
           }
           if (label != null) {
               condition.put("label", label);
           }
           if (date != null) {
               condition.put("date", date);
           }
           if (uid != null) {
               condition.put("uid", uid);
           }
           if (activityId != null) {
               condition.put("activityId", activityId);
           }
           return scheduleService.findScheduleDetails(condition, pageNum, pageSize);
    }


    /*查询当周日程*/
    @GetMapping("/getWeek")
    public Map<String, List<ScheduleView>> getWeekSchedule(
            @RequestParam Long uid,@RequestParam String date) throws ScheduleAdminException{
        return scheduleService.getWeekSchedule(uid,date);
    }
    /*根据id查询日程*/
    @GetMapping("/{id}")
   @PreAuthorize("hasRole('normal')")
    public Schedule getScheduleById(@PathVariable Long id){
        return scheduleService.getScheduleById(id);
    }

    /*查询所有标签*/
    @GetMapping("/getLabel")
    @PreAuthorize("hasRole('normal')")
    public List<String> getScheduleLabel(){
        return scheduleService.findScheduleLabel();
    }
}

